package com.kzd.gtgame.main.money

import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.KEY_MONEY_TASK_TYPE
import com.kzd.gtgame.main.money.adapter.MoneyTaskListAdapter
import com.kzd.gtgame.main.popup.SystemTipsPopup
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_task.*

class MoneyTaskActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mSystemTipsPopup: SystemTipsPopup by lazy {
        SystemTipsPopup(context = this)
    }

    private val mMoneyTaskListAdapter = MoneyTaskListAdapter()

    private val mType: String by lazy {
        intent?.extras?.getString(KEY_MONEY_TASK_TYPE) ?: ""
    }

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_task

    override fun stateBar(): StatusBar {
        return StatusBar(view = tlMoneyTaskBar)
    }

    override fun initView() {
        rvMoneyTaskBody.adapter = mMoneyTaskListAdapter

        mMoneyTaskListAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mMoneyTaskListAdapter.getItem(position)
            if (view.id != R.id.tvMoneyTaskListGo) {
                return@setOnItemChildClickListener
            }
            if (itemData.verify_rs == 1) {
                viewModel.moneyTaskDayGet(itemData.type)
            } else if (itemData.verify_rs == 0 && itemData.type == "today_login") {
                mSystemTipsPopup.setMessage("每日登录游戏就可领取")
                    .showPopupWindow()
            } else if (itemData.verify_rs == 0 && itemData.type == "today_recharge") {
                mSystemTipsPopup.setMessage("仅计算现金/平台币充值游戏")
                    .showPopupWindow()
            } else if (itemData.verify_rs == 0 && itemData.type == "today_share") {
                viewModel.userShare(
                    activity = this, url = itemData.url,
                    title = "${getString(R.string.app_name)}--领1000元",
                    desc = "注册即享千元福利，千款手游特权"
                )
            } else if (itemData.verify_rs == 0 && itemData.type == "today_pinglun") {
                mSystemTipsPopup.setMessage("去评论，评论成功即可获取奖励")
                    .showPopupWindow()
            }
        }
    }

    override fun initData() {
        if (mType == "newbieTask") {
            viewModel.moneyTaskNew()
        } else if (mType == "todayTask") {
            viewModel.moneyTaskDay()
        }
    }

    override fun initViewMode() {
        viewModel.moneyTaskLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyTaskListAdapter.setNewInstance(data)
        })

        viewModel.moneyTaskDayGetLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            initData()
        })

        viewModel.userIsShareLiveData.observe(this as LifecycleOwner, {
            viewModel.moneyTaskAddShare()
        })
    }
}